<?php
class bingchuang extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的


		$this->tit = '病床';
		$this->modelObj = D('company_bingchuang');
		$this->adminObj = D('company_admin');

		$this->areaObj = D('company_area');
		$this->cateObj = D('company_cate');
		$this->zoneObj = D('company_zone');

		$this->area_list = $this->areaObj->get_TreeList("status=1 ".$this->cond_base);
		$this->cate_list = $this->cateObj->get_TreeList("status=1 ".$this->cond_base);
		$this->zone_list = $this->zoneObj->get_TreeList("status=1 ".$this->cond_base);


		$this->appid = 'wx913120c9717ff5d3';
    	$this->appsecret = '8a284b6f7fbeaa91007f036363eae369';

	}

    //列表
	public function index(){
		$this->tabtit = array('全部','隐藏');
		$cond = "1=1".$this->cond_base;
		//加入查询条件
		switch($this->tab){
			case '1':
				$cond .=" and status=0";
				break;
			default:
				$cond .=" and status=1";
		}
		$result = $this->modelObj->get_pagelist($cond);

		if($result){
			$this->zones = $this->zoneObj->ids($result['list'],'zone_id');
			// dump($this->areas);exit;
			$this->list = $result['list'];
			$this->showPage = $result['showPage'];
			$this->total = $result['total'];
		}

	}

	//添加
	public function add(){
		if($this->isAjax()){
			$data = $this->postData('data');
			$data['company_id'] = $this->company['id'];
			$res = $this->modelObj->add($data);
			$this->renderSuccess('添加成功', "/{$this->routeUrl}/index");
		}
		$this->info = array('sort'=>100,'status'=>1);
		$this->display($this->routeUrl."/set.php");
	}

	//编辑
	public function edit(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');
			$data['company_id'] = $this->company['id'];
			$this->modelObj->edit($id,$data);
			$this->renderSuccess('编辑成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->get_info($id);
		// dump($this->info);exit;
		$this->display($this->routeUrl."/set.php");
	}

    //审核
	public function act()
	{
		$id = arg('id');
		$status = arg('status');
		$msg = ($status==1)?'显示':'隐藏';
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->act($id,$status);
				$this->renderSuccess($msg.'成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->act($val,1);
					}
					$this->renderSuccess('显示成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
		}
	}

    //删除
	public function del()
	{
		$id = arg('id');
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->del($id);
				$this->renderSuccess('删除成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->del($val);
					}
					$this->renderSuccess('删除成功');
				}else{
					$this->renderError('请选择数据');
				}
			}

		}
	}

	//隐藏
	public function ban()
	{
		$opid = arg('opid');
		if($this->isAjax()){
			if($opid){
				foreach($opid as $val){
					$res = $this->modelObj->act($val,0);
				}
				$this->renderSuccess('隐藏成功');
			}
			$this->renderError('请选择数据');
		}
	}

	//排序
	public function sort()
	{
        $sort = arg('sort');
		$result = $this->modelObj->sort($sort);
		$this->renderSuccess('排序成功');
	}


	 //审核
	public function make()
	{
		if($this->isAjax()){

			$data = $this->postData('data');
			$num_s = $data['num_s'];
			$num_e = $data['num_e'];
			if($num_s && $num_e){
				for($i=$num_s;$i<=$num_e;$i++){
					$data = array(
						'company_id' => $this->company['id'],
						'zone_id' =>$data['zone_id'],
						'name' => $i.'床',
						'add_time' => date('Y-m-d H:i:s'),
						'status'=>1,
					);
					$bc_id = $this->modelObj->add($data);

					$url = $this->setting['site']['site_url'].'/index/diaocha/add?bc_id='.$bc_id.'&company_id='.$this->company['id'];
					$info = $this->modelObj->findById($bc_id);
					$res = $this->modelObj->qrcode($url,$info,$this->company,'bingchuang');
					// $this->renderSuccess('生成二维码成功');
				}

				$this->renderSuccess('生成成功');
			}
		}

		$this->info = ['status'=>1,'num_s'=>1,'num_e'=>80];

		$this->layout = "admin/layout2.php";
	}


	//排序
	public function qrcode1()
	{
		require(APP_DIR.'/protected/include/lib/phpqrcode.php');

        $id = arg('id');
		$status = arg('status');
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$url = $this->setting['site']['site_url'].'/index/diaocha/add?bc_id='.$id.'&company_id='.$this->company['id'];
				$info = $this->modelObj->findById($id);
				$res = $this->modelObj->qrcode($url,$info,$this->company,'bingchuang');
				$this->renderSuccess('生成二维码成功');
			}else{
				if($opid){

					foreach($opid as $val){
						$url = $this->setting['site']['site_url'].'/index/diaocha/add?bc_id='.$val.'&company_id='.$this->company['id'];
						$info = $this->modelObj->findById($val);
						$res = $this->modelObj->qrcode($url,$info,$this->company,'bingchuang');
					}
					$this->renderSuccess('生成二维码成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
		}
	}

	//排序
	public function qrcode2()
	{
		// require(APP_DIR.'/protected/include/lib/phpqrcode.php');

        $id = arg('id');
		$status = arg('status');
		$opid = arg('opid');
		if($this->isAjax()){

			$WxQrcode = new \lib\WxQrcode($this->appid,$this->appsecret);

			if($id){


				$url = $WxQrcode->getEwm($id);

				// $url = $this->setting['site']['site_url'].'/index/login/reg?zone_id='.$id;
				$info = $this->modelObj->findById($id);
				$res = $this->modelObj->DownLoadQr2($url,$info,$this->company,'weixin');
				$this->renderSuccess('生成二维码成功');
			}else{
				if($opid){

					foreach($opid as $val){
						$url = $WxQrcode->getEwm($val);
						// $url = $this->setting['site']['site_url'].'/index/login/reg?zone_id='.$val;
						$info = $this->modelObj->findById($val);
						$res = $this->modelObj->DownLoadQr2($url,$info,$this->company,'weixin');
					}
					$this->renderSuccess('生成二维码成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
		}
	}


	//排序
	public function zipdown()
	{

        $filePath = "./upload/company_{$this->company['id']}/weixin/";
		$zipFileName = "{$this->company['id']}_weixin.zip";

		$zip = new \lib\ZipService();
		$zip = $zip->createZip($filePath, $zipFileName);

		$file = $zipFileName;
		if (file_exists($file)) {
			header('Content-Description: File Transfer');
			header('Content-Type: application/zip');
			header('Content-Disposition: attachment; filename="'.basename($file).'"');
			header('Expires: 0');
			header('Cache-Control: must-revalidate');
			header('Pragma: public');
			header('Content-Length: ' . filesize($file));
			readfile($file);
			@unlink($file);
			exit;
		}
	}


}